package fun._6742.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import fun._6742.exception.CustomException;
import fun._6742.interceptor.LoginInterceptor;
import fun._6742.mapper.ProductOrderMapper;
import fun._6742.model.dataobject.ProductOrderDO;
import fun._6742.model.valueobject.LoginUser;
import fun._6742.model.valueobject.OrderVO;
import fun._6742.request.ConfirmOrderRequest;
import fun._6742.service.ProductOrderItemService;
import fun._6742.service.ProductOrderService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuQ
 * @since 2022-09-22
 */
@Slf4j
@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrderDO> implements ProductOrderService {

    @Resource
    ProductOrderItemService productOrderItemService;


    /**
     * * 防重提交 TODO
     * * 锁定商品库存
     * * 创建订单对象
     * * 创建子订单对象
     * * 清理购物车
     *
     * @param confirmOrderRequest
     * @return
     */
    @Override
    public Long createOrder(ConfirmOrderRequest confirmOrderRequest) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();

//        创建订单对象
        ProductOrderDO productOrderDO = new ProductOrderDO();
        BeanUtil.copyProperties(confirmOrderRequest,productOrderDO);
        //        TODO 待规范
        productOrderDO.setState("NEW");
        productOrderDO.setReceiverAddress(confirmOrderRequest.getAddressId());
        productOrderDO.setCreateTime(new Date());
        productOrderDO.setUserId(loginUser.getId());
        productOrderDO.setNickname(loginUser.getName());
        productOrderDO.setOrderType("DAILY");
        save(productOrderDO);
        return productOrderDO.getId();
    }


    @Override
    public Map<String, Object> findOne(long id) {
        ProductOrderDO orderDO = getById(id);
        if (orderDO == null) {
            log.error("findOrder:res:false,id:{}", id);
            throw new CustomException(HttpStatus.NOT_FOUND, "order(id=" + id + " does not exist");
        }
        OrderVO orderVO = new OrderVO();
        BeanUtil.copyProperties(orderDO, orderVO);
        Map<String, Object> result = new HashMap<>();
        result.put("order", orderVO);
        result.put("items", productOrderItemService.listAllItems(id));
        return result;
    }

    @Override
    public Map<String, Object> listAll(int page, int size) {
        IPage<ProductOrderDO> orderDOIPage = page(new Page<>(page, size));
        Map<String, Object> map = new HashMap<>(3);
        map.put("total_page", orderDOIPage.getPages());
        map.put("total_record", orderDOIPage.getTotal());
        map.put("data", orderDOIPage.getRecords().stream().map(obj -> {
            OrderVO orderVO = new OrderVO();
            BeanUtil.copyProperties(obj, orderVO);
            Map<String, Object> result = new HashMap<>();
            result.put("order", orderVO);
            result.put("items", productOrderItemService.listAllItems(orderVO.getId()));
            return result;
        }).collect(Collectors.toList()));
        return map;
    }
}
